package cell 
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import ru.gosoplata.model.TmpModel;
	
	/**
	 * ...
	 * @author a.orleansky
	 */
	public class KeyBoard extends Sprite 
	{
		public static var MAGIC:Boolean = false;
		
		private var btn_types1:Array = new Array(new Array("","ёЁ"),new Array("1","!"),new Array("2","@"),new Array("3","№"),new Array("4",";"),new Array("5","%"),new Array("6",":"),new Array("7","?"),new Array("8","*"),new Array("9","("),new Array("0",")"),new Array("-","_"),new Array("=","+"),new Array("backspace",""));
		private var btn_types2:Array = new Array(new Array("tab",""),new Array("qQ","йЙ"),new Array("wW","цЦ"),new Array("eE","уУ"),new Array("rR","кК"),new Array("tT","еЕ%"),new Array("yY","нН"),new Array("uU","гГ"),new Array("iI","шШ"),new Array("oO","щЩ"),new Array("pP","зЗ"),new Array("","хХ"),new Array("'","ъЪ"),new Array("|","/"));
		private var btn_types3:Array = new Array(new Array("capslock",""),new Array("aA","фФ"),new Array("sS","ыЫ"),new Array("dD","вВ"),new Array("fF","аА"),new Array("gG","пП"),new Array("hH","рР"),new Array("jJ","оО"),new Array("kK","лЛ"),new Array("lL","дД"),new Array("\"","жЖ"),new Array("?","эЭ"),new Array("enter",""));
		private var btn_types4:Array = new Array(new Array("shift",""),new Array("zZ","яЯ"),new Array("xX","чЧ"),new Array("cC","сС"),new Array("vV","мМ"),new Array("bB","иИ"),new Array("nN","тТ"),new Array("mM","ьЬ"),new Array("","бБ"),new Array("","юЮ"),new Array(",","."),new Array("magic",""));
		private var btn_types5:Array = new Array(new Array("lang", ""), new Array("spacebar", ""), new Array("clear", ""));
		
		private var entered_str:String;
		private var limit:Number = 30;
		private var pic_count:Number;
		
		private var lang_pic:Bitmap;
		private var lang_pic_sprite:Sprite;
		//private var rus_data:BitmapData;
		//private var eng_data:BitmapData;
		private var lang_txt:TextField;
		
		private	var delta2:Number = 2;
		private	var delta3:Number = 2;
		private	var delta4:Number = 2;
		private	var delta5:Number = 2;
		
		private var curr_lang:String = "rus";
		private var curr_shift:Boolean = false;
		private var curr_cups:Boolean = false;
		
		private var testfieldtxt:TextField = new TextField();
		
		public var current_lang:String = "rus";
		public var btns:Array;
		public var btns2:Array;
		public var btns3:Array;
		public var btns4:Array;
		public var btns5:Array;
		
		public function KeyBoard() 
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public function set num_limit(num:Number):void {
			limit = num;
		}
		
		public function get entered_value():String {
			return entered_str;
		}
		
		public function set rewrite_value(str:String):void {
			entered_str = str;
		}
		
		public function init(e:Event=null):void {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			entered_str = "";
			testfieldtxt.text = "";
			testfieldtxt.restrict = null;
			pic_count = 0;
			placeTF();
			createKeyPad();
		}
		
		
		public function clearBuffer():void {
			entered_str = "";
		}
		
		public function clearEnteredValue():void {
			entered_str = "";
		}
		
		public function reDrawKeyBoard():void {
				for (var ii:Number = 0; ii < btns.length; ii++ ) {
					var btn:KeyBoardBtn = btns[ii];
					btn.reDrawTextOnBtns(curr_lang, curr_shift,curr_cups);
				}
				for (var jj:Number = 2; jj <= 5; jj++ ) {
					var _btns:Array = this["btns" + jj];
					for ( ii = 0; ii < _btns.length; ii++ ) {
						 btn = _btns[ii];
						btn.reDrawTextOnBtns(curr_lang, curr_shift,curr_cups);
					}
				}
			
		}
		
		public function setRestrickt(str:String):void { 
			testfieldtxt.restrict = str;
		}
		
		public function nullRestrickt():void {
			testfieldtxt.restrict = null;
		}
		public function onPressbBtn(e:Event):void {
			var btn:KeyBoardBtn = (e.target as KeyBoardBtn);
			MAGIC = false;
			switch(btn.indx) {
				case "clear":
					entered_str = "";
					break;
				case "capslock":
					curr_cups = !curr_cups;
					curr_shift = false;
					break;
				case "shift":
					curr_shift = !curr_shift;
					break;
				case "lang":
					curr_lang = (curr_lang == "eng"?"rus":"eng");
					lang_pic.bitmapData = TmpModel.loadedImages.getBitmapDataByName("kb_icon_lang_"+curr_lang).bitmapData;
					break;
				case "spacebar":
					if(entered_str.length < limit) entered_str += " ";
					break;
				case "enter":
					break;
				case "magic":
					MAGIC = true;
					break;
				case "backspace":
					entered_str = entered_str.slice(0, (entered_str.length-1));
					break;
				default:
					if (entered_str.length < limit) {
						testfieldtxt.appendText(btn.indx);
						entered_str += btn.indx;
					}
					curr_shift = false;
					break;
			}
			reDrawKeyBoard();
			dispatchEvent(new Event(KeyPad.KEY_PAD_RELEASED,true));
		}
		private function placeTF():void 
		{
			var txt_type:TextFormat = TmpModel.getTextFormat(18, 0x676767,true);
			txt_type.letterSpacing = 7;

			lang_txt = new TextField();
			lang_txt.embedFonts = true;
			lang_txt.y = -35;
			lang_txt.x = stage.stageWidth - 250;
			lang_txt.width = 90;
			lang_txt.height = 75;
			lang_txt.border = false;
			lang_txt.selectable = false;
			lang_txt.defaultTextFormat = txt_type;
			lang_txt.antiAliasType = AntiAliasType.ADVANCED;
			lang_txt.text = "Язык: ";
			
			lang_pic = new Bitmap();
			lang_pic.bitmapData = TmpModel.loadedImages.getBitmapDataByName("kb_icon_lang_"+curr_lang).bitmapData;
			lang_pic.x = 0;
			lang_pic.y = 0;
			addChild(lang_txt);
			
			lang_pic_sprite = new Sprite();
			lang_pic_sprite.x = lang_txt.x + lang_txt.width;
			lang_pic_sprite.y = lang_txt.y;
			lang_pic_sprite.addChild(lang_pic);
			addChild(lang_pic_sprite);
			
			lang_pic_sprite.addEventListener(MouseEvent.CLICK, onLangClick);
			lang_txt.addEventListener(MouseEvent.CLICK, onLangClick);
		}
		
		private function onLangClick(e:MouseEvent):void 
		{
			curr_lang = (curr_lang == "eng"?"rus":"eng");
			switchLanguageTo(curr_lang);
		}
		
		private function allPicLoaded(e:Event):void 
		{
			pic_count--;
			if (pic_count == 0) {
				var delta:Number = 2+(22/(btns.length-1));
				 delta2 = 2+(19/(btns2.length-1));
				 delta3 = 2+(17/(btns3.length-1));
				 delta4 = 2+(16/(btns4.length-1));
				 delta5 = 2;
				for (var ii:Number = 0; ii < btns.length; ii++ ) {
					var btn:KeyBoardBtn = btns[ii];
					var prev_btn:KeyBoardBtn = (ii > 0)?btns[ii - 1]:btns[ii];
					if (ii == 0) btn.x = 0;
					else btn.x = (prev_btn.width+delta)+prev_btn.x;
					btn.y = 0;
				}
				for (var jj:Number = 2; jj <= 5; jj++ ) {
					var _btns:Array = this["btns" + jj];
					for ( ii = 0; ii < _btns.length; ii++ ) {
						 btn = _btns[ii];
						 prev_btn = (ii > 0)?_btns[ii - 1]:_btns[ii];
						if (ii == 0) btn.x = 0;
						else btn.x = (prev_btn.width+this["delta"+jj])+prev_btn.x;
						btn.y = (_btns[0].height+2)*(jj-1);
					}
				}
				//switchLanguageTo("rus");
			}
		}
		
		public function switchLanguageTo(lang:String):void {
			curr_lang = lang;
			lang_pic.bitmapData = TmpModel.loadedImages.getBitmapDataByName("kb_icon_lang_"+curr_lang).bitmapData;
			reDrawKeyBoard();
		}
		
		public function pressShift():void {
			curr_shift = true;
			reDrawKeyBoard();
		}
		
		public function createKeyPad():void {
			btns = new Array();
			pic_count = btn_types1.length;// +btn_types2.length + btn_types3.length + btn_types4.length + btn_types5.length;
			for (var ii:Number = 0; ii < btn_types1.length; ii++ ) {
				var btn:KeyBoardBtn = new KeyBoardBtn();
				btn.init(btn_types1[ii]);
				btn.addEventListener(KeyBoardBtn.KEY_PAD_BTN_CLICK, onPressbBtn);
				btn.addEventListener(KeyBoardBtn.KEY_PAD_BTN_LOADED, allPicLoaded);
				btns[ii] = btn;
				addChild(btns[ii]);
			}
			for (var jj:Number = 2; jj <= 5;jj++ ){
				this["btns"+jj] = new Array();
				pic_count += this["btn_types"+jj].length;
				for (ii = 0; ii < this["btn_types"+jj].length; ii++ ) {
					 btn = new KeyBoardBtn();
					btn.init(this["btn_types" + jj][ii]);
					//pic_count++;
					btn.addEventListener(KeyBoardBtn.KEY_PAD_BTN_CLICK, onPressbBtn);
					btn.addEventListener(KeyBoardBtn.KEY_PAD_BTN_LOADED, allPicLoaded);
					
					this["btns"+jj][ii] = btn;
					addChild(this["btns"+jj][ii]);
				}
			}
		}
	}

}